Try

sealed class Try<out T>

Representation of an operation that might successfully return a value or throw an exception.

An instance of Try may be either a Success or a Failure.

This type is based on scala.util.Try.

Note: Only non-fatal exceptions are caught by the combinators on Try (see NonFatal). Serious system errors, on the other hand, will be thrown.

Parameters

T

Type of the value of a successful operation.

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
abstract fun filter(predicate: (T) -> Boolean): Try<T>

Returns the same Success if the predicate is satisfied for the value. Otherwise returns a Failure.

Link copied to clipboard
inline fun <R> filterIsInstance(): Try<R>

Returns the same Success casted to type R if it is R. Otherwise returns a Failure.

Link copied to clipboard
abstract fun filterNot(predicate: (T) -> Boolean): Try<T>

Returns the same Success if the predicate is not satisfied for the value. Otherwise returns a Failure.

Link copied to clipboard
abstract fun filterOrElse(predicate: (T) -> Boolean, throwable: (T) -> Throwable): Try<T>

Returns the same Success if the predicate is satisfied for the value. Otherwise returns a Failure containing the given throwable.

Link copied to clipboard
abstract fun <R> flatMap(transform: (T) -> Try<R>): Try<R>

Maps value of a Success to a new Try using transform or returns the same Try if this is a Failure.

Link copied to clipboard
inline fun <R> fold(successTransform: (T) -> R, failureTransform: (Throwable) -> R): R

Transforms a Success using successTransform or a Failure using failureTransform.

Link copied to clipboard
abstract fun forEach(action: (T) -> Unit)

Runs action if this is a Success. Returns Unit without any action if this is a Failure.

Link copied to clipboard
abstract fun get(): T

Gets the value of a Success or throw an exception from a Failure.

Link copied to clipboard
abstract fun getOrNull(): T?

Gets the value of a Success or null if this is a Failure.

Link copied to clipboard
abstract fun <R> map(transform: (T) -> R): Try<R>

Maps value of a Success using transform or returns the same Try if this is a Failure.

Link copied to clipboard
abstract fun toEither(): Either<Throwable, T>

Converts this Try to Either.

Link copied to clipboard
abstract fun toOption(): Option<T>

Converts this Try to Option.

Link copied to clipboard
inline fun <R> transform(successTransform: (T) -> Try<R>, failureTransform: (Throwable) -> Try<R>): Try<R>

Transforms a Success using successTransform or a Failure using failureTransform.

Link copied to clipboard
infix fun <R> zip(other: Try<R>): Try<Pair<T, R>>

Returns Success containing a Pair of values of this and other if both instances of Try are Success. Otherwise returns first Failure.

inline fun <T1, R> zip(other: Try<T1>, transform: (T, T1) -> R): Try<R>

Returns Success containing the result of applying transform to both values of this and other if both instances of Try are Success. Otherwise returns first Failure.

Properties

Link copied to clipboard
abstract val failed: Try<Throwable>

Returns a Success with an exception it this is a Failure or a Failure if this is a Success.

Link copied to clipboard
abstract val isFailure: Boolean

Returns true if this is a Failure or false if this is Success.

Link copied to clipboard
abstract val isSuccess: Boolean

Returns true if this is a Success or false if this is Failure.

Inheritors

Link copied to clipboard
Link copied to clipboard

Extensions

Link copied to clipboard
fun <T> Try<Option<T>>.evert(): Option<Try<T>>

Moves inner Option outside of the outer Try.

Link copied to clipboard
fun <T> Try<T?>.filterNotNull(): Try<T>

Returns the same Success if its value is not null. Otherwise returns a Failure.

Link copied to clipboard
fun <T> Try<Option<T>>.flatten(): Option<T>

Extracts an Option nested in the Try to a not nested Option.

fun <T> Try<Try<T>>.flatten(): Try<T>

Transforms a nested Try to a not nested Try.

Link copied to clipboard
inline fun <T> Try<T>.getOrElse(default: () -> T): T

Gets the value of a Success or default value if this is a Failure.

Link copied to clipboard
inline fun <T> Try<T>.orElse(default: () -> Try<T>): Try<T>

Returns this Try if this is a Success or default if this is a Failure.

Link copied to clipboard
inline fun <T> Try<T>.recover(rescue: (Throwable) -> T): Try<T>

Returns this Try if this is a Success or a Try created for the rescue operation if this is a Failure.

Link copied to clipboard
inline fun <T> Try<T>.recoverWith(rescue: (Throwable) -> Try<T>): Try<T>

Returns this Try if this is a Success or a Try created by the rescue function if this is a Failure.